package com.exam.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import com.exam.exception.ScheduleIsExistException;
import com.exam.mapper.ClassesMapper;
import com.exam.mapper.MajorMapper;
import com.exam.mapper.ScheduleMapper;
import com.exam.mapper.SubjectMapper;
import com.exam.pojo.Classes;
import com.exam.pojo.Major;
import com.exam.pojo.Paging;
import com.exam.pojo.Schedule;
import com.exam.pojo.Subject;
import com.exam.service.ScheduleService;

/**
 * @author 郭荣锋
 * 考试安排服务层实现类
 */
@Service("scheduleServiceImpl")
public class ScheduleServiceImpl implements ScheduleService,ApplicationListener<ContextRefreshedEvent> {

	@Resource(name="majorMapper")
	private MajorMapper majorMapper;
	@Resource(name="scheduleMapper")
	private ScheduleMapper scheduleMapper;
	@Resource(name="subjectMapper")
	private SubjectMapper subjectMapper;
	@Resource(name="classesMapper")
	private ClassesMapper classesMapper;
	@Resource(name="schedule")
	private Schedule schedule;
	@Resource(name="paging")
	private Paging<Schedule> paging;
	
	/**
	 * 添加考试安排
	 * @param subjectName 学科名称
	 * @param classesName 班级名称
	 * @param majorName 专业名称
	 * @param dates 考试时间
	 * @throws ScheduleIsExistException 考试安排已存在异常
	 */
	public void addSchedule(String subjectName,String classesName,
			String majorName,String dates) throws ScheduleIsExistException {
		//获取classesId和subjectId
		Subject subject = subjectMapper.getSubjectByName(subjectName);
		Major major = majorMapper.getMajorByName(majorName);
		Map map = new HashMap();
		map.put("majorId",major.getId());
		map.put("classesName",classesName);
		Classes classes = classesMapper.getClassesByMajorIdAndName(map);
		schedule.setClasses(classes);
		schedule.setDates(dates);
		schedule.setState(0);
		schedule.setSubject(subject);
		if (null != scheduleMapper.scheduleIsExist(schedule)) { //判断考试安排是否存在
			throw new ScheduleIsExistException();
		} else { //如果不存在，则将考试安排存入数据库
			scheduleMapper.addSchedule(schedule);
		}
	}
	
	/**
	 * 考试安排表初始化
	 */
	@Override
	public void scheduleInit() {
		scheduleMapper.beforeExam();
		scheduleMapper.exam();
		scheduleMapper.afterExam();
	}

	/**
	 *  服务器启动时执行的方法
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		scheduleInit(); //调用考试安排表初始化函数
	}

	/**
	 * 根据不同条件获取考试安排
	 * @param map 必填 page pageSize 可选
	 * @return 存着考试安排列表的Paging对象
	 */
	public Paging<Schedule> getSchedule(Map<String,Object> map) {
		Integer page = (Integer)map.get("page");
		Integer pageSize = (Integer)map.get("pageSize");
		map.put("start",(page-1)*pageSize);
		map.put("offset",pageSize);
		paging.setCurrentPage(page);
		paging.setPageSize(pageSize);
		paging.setTotalCount(scheduleMapper.getTotalCount(map));
		paging.setList(scheduleMapper.getSchedule(map));
		return paging;
	}
	
	/**
	 * 删除考试安排
	 * @param scheduleId 考试安排编号
	 */
	public void deleteSchedule(Integer scheduleId) {
		scheduleMapper.deleteSchedule(scheduleId);
	}
	
	/**
	 * 通过考试安排编号获取考试安排对象
	 * @param scheduleId 考试安排编号
	 * @return 考试安排对象
	 */
	public Schedule getScheduleById(Integer scheduleId) {
		return scheduleMapper.getScheduleById(scheduleId);
	}

	public List<Schedule> getScheduleByClassesId(Integer classesId) {
		return scheduleMapper.getScheduleByClassesId( classesId );
	}
}
